{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "简洁实现softmax回归\n",
    "'''\n",
    "%matplotlib inline\n",
    "import d2lzh as d2l\n",
    "from mxnet import gluon, init\n",
    "from mxnet.gluon import loss as gloss, nn\n",
    "from mxnet import nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 256\n",
    "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "定义和初始化模型\n",
    "添加⼀个输出个数为10的全连接层\n",
    "使⽤均值为0、标准差为0.01的正态分布随机初始化模型的权重参数\n",
    "'''\n",
    "net = nn.Sequential()\n",
    "net.add(nn.Dense(10))\n",
    "net.initialize(init.Normal(sigma=0.01))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "softmax和交叉熵损失函数\n",
    "'''\n",
    "loss = gloss.SoftmaxCrossEntropyLoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "定义优化算法\n",
    "使⽤学习率为0.1的小批量随机梯度下降作为优化算法\n",
    "'''\n",
    "trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.0618})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 0.4297, train acc 0.854, test acc 0.854\n",
      "epoch 2, loss 0.4279, train acc 0.854, test acc 0.852\n",
      "epoch 3, loss 0.4260, train acc 0.855, test acc 0.851\n",
      "epoch 4, loss 0.4253, train acc 0.855, test acc 0.854\n",
      "epoch 5, loss 0.4239, train acc 0.855, test acc 0.850\n",
      "epoch 6, loss 0.4229, train acc 0.856, test acc 0.854\n",
      "epoch 7, loss 0.4217, train acc 0.855, test acc 0.856\n",
      "epoch 8, loss 0.4204, train acc 0.857, test acc 0.855\n",
      "epoch 9, loss 0.4198, train acc 0.857, test acc 0.854\n",
      "epoch 10, loss 0.4188, train acc 0.857, test acc 0.852\n",
      "epoch 11, loss 0.4176, train acc 0.858, test acc 0.854\n",
      "epoch 12, loss 0.4170, train acc 0.858, test acc 0.855\n",
      "epoch 13, loss 0.4160, train acc 0.858, test acc 0.856\n",
      "epoch 14, loss 0.4152, train acc 0.858, test acc 0.855\n",
      "epoch 15, loss 0.4144, train acc 0.859, test acc 0.857\n",
      "epoch 16, loss 0.4136, train acc 0.858, test acc 0.857\n",
      "epoch 17, loss 0.4130, train acc 0.859, test acc 0.856\n",
      "epoch 18, loss 0.4121, train acc 0.859, test acc 0.856\n",
      "epoch 19, loss 0.4118, train acc 0.859, test acc 0.857\n",
      "epoch 20, loss 0.4110, train acc 0.860, test acc 0.857\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "训练模型\n",
    "'''\n",
    "num_epochs = 20\n",
    "d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None,\n",
    "             None, trainer)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
